class Hero{
    private String name;
    private int hp;
    private int attack;
    private int defense;

    public Hero(String name,int hp,int attack,int defense){
        this.name=name;
        this.hp=hp;
        this.attack=attack;
        this.defense=defense;
    }

    public String getName(){
        return name;
    }

    public int getHp(){
        return hp;
    }

    public void setHp(int hp){
        this.hp=hp;
    }

    public int getAttack(){
        return attack;
    }

    public int getDefense(){
        return defense;
    }

    public void skill(){
        System.out.println(name+"使用普通攻击");
    }

    public void ultimate(){
        System.out.println(name+"释放了大招");
    }

    public void attack(Hero enemy){
        int damage=this.attack-enemy.getDefense();
        if (damage>0){
            enemy.setHp(enemy.getHp()-damage);
            System.out.println(name+"对"+enemy.getName()+"造成了"+damage+"点伤害");
        }else {
            System.out.println(name+"的攻击被"+enemy.getName()+"格挡了");
        }
    }
}

class Tank extends Hero{
    private int shield;

    public Tank(String name,int hp,int attack,int defense,int shield){
        super(name,hp,attack,defense);
        this.shield=shield;
    }

    @Override
    public void skill(){
        System.out.println(getName()+"开启护盾，获得"+shield+"点护盾值");
    }

    @Override
    public void ultimate(){
        System.out.println(getName()+"释放终极技能：群体嘲讽！");
    }
}

class Marksman extends Hero{
    private int criticalRate;

    public Marksman(String name,int hp,int attack,int defense,int criticalRate){
        super(name, hp, attack, defense);
        this.criticalRate=criticalRate;
    }

    @Override
    public void skill(){
        System.out.println(getName()+"使用穿透射击");
    }

    @Override
    public void ultimate(){
        System.out.println(getName()+"释放终极技能：箭雨覆盖！");
    }

    public void criticalAttack(Hero enemy){
        int damage=(int)(getAttack()*(1+criticalRate/100.0))- enemy.getDefense();
        if (damage>0){
            enemy.setHp(enemy.getHp()-damage);
            System.out.println(getName()+"对"+enemy.getName()+"造成了"+damage+"点伤害");
        }
    }
}


class Mage extends Hero{
    private int magicPower;

    public Mage(String name,int hp,int attack,int defense,int magicPower){
        super(name,hp,attack,defense);
        this.magicPower=magicPower;
    }

    @Override
    public void skill(){
        System.out.println(getName()+"释放法术弹");
    }

    @Override
    public void ultimate(){
        System.out.println(getName()+"释放了终极技能：元素风暴！");
    }

    public void castSpell(Hero enemy){
        int damage=magicPower- enemy.getDefense()/2;
        if (damage > 0) {
            enemy.setHp(enemy.getHp()-damage);
            System.out.println(getName()+"对"+enemy.getName()+"释放法术，造成"+damage+"点魔法伤害");
        }
    }
}

public class JavaKE3 {
    public static void main(String[] args) {
        // 向上转型（多态）
        Hero tank = new Tank("张飞", 5000, 200, 300, 1000);
        Hero marksman = new Marksman("后羿", 2500, 400, 100, 50);
        Hero mage = new Mage("安琪拉", 2000, 350, 80, 500);

        // 调用方法（多态的表现）
        battle(tank);
        battle(marksman);
        battle(mage);

        System.out.println("\n=== 战斗开始 ===");
        tank.attack(marksman);
        marksman.skill();
        ((Marksman)marksman).criticalAttack(tank); // 向下转型调用子类特有方法
        ((Mage)mage).castSpell(tank);
        tank.ultimate();
    }

    // 多态方法
    public static void battle(Hero hero) {
        System.out.println("\n" + hero.getName() + "进入战场：");
        hero.skill();
        hero.ultimate();
    }
}